﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace MailQueueUmbracoAdmin
{
    public partial class MailQueueSettings : System.Web.UI.UserControl
    {
        private string smtpserver = String.Empty;
        private string smtpuser = String.Empty;
        private string smtppass = String.Empty;
        private int smtpport = 25;
        private bool smtpssl = false;
        private string smtpdummy = String.Empty;
        private string smtponbehalf = String.Empty;
        private bool pause = false;
        private int maxbactchsize = 0;
        private int sendinterval = 30;
        private int retries = 0;
        private int retryinterval = 5;
        private int purgeafterdays = 90;
        private string settingsname = String.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            tbSmtpPassword.Attributes.Add("autocomplete", "off");
            if (!Page.IsPostBack)
            {
                GetSettings();
                DisplaySettings();
            }
        }

        private void EnablePause()
        {
            pauseButton.ImageUrl = "/umbraco/images/MailQueue/Pause-Normal-48x48.png";
            pauseButton.Attributes.Add("onmouseover", "this.src='/umbraco/images/MailQueue/Pause-Hot-48x48.png'");
            pauseButton.Attributes.Add("onmouseout", "this.src='/umbraco/images/MailQueue/Pause-Normal-48x48.png'");
            pauseButton.Attributes.Add("onmousedown", "this.src='/umbraco/images/MailQueue/Pause-Pressed-48x48.png'");
            pauseButton.Attributes.Add("onmouseup", "this.src='/umbraco/images/MailQueue/Pause-Hot-48x48.png'");
            pauseButton.Enabled = true;
        }

        private void EnableStart()
        {
            startButton.ImageUrl = "/umbraco/images/MailQueue/Play-Normal-48x48.png";
            startButton.Attributes.Add("onmouseover", "this.src='/umbraco/images/MailQueue/Play-Hot-48x48.png'");
            startButton.Attributes.Add("onmouseout", "this.src='/umbraco/images/MailQueue/Play-Normal-48x48.png'");
            startButton.Attributes.Add("onmousedown", "this.src='/umbraco/images/MailQueue/Play-Pressed-48x48.png'");
            startButton.Attributes.Add("onmouseup", "this.src='/umbraco/images/MailQueue/Play-Hot-48x48.png'");
            startButton.Enabled = true;
        }

        private void DisableStart()
        {
            startButton.Attributes.Remove("onmouseover");
            startButton.Attributes.Remove("onmouseout");
            startButton.Attributes.Remove("onmousedown");
            startButton.Attributes.Remove("onmouseup");
            startButton.ImageUrl = "/umbraco/images/MailQueue/Play-Disabled-48x48.png";
            startButton.Enabled = false;
        }

        private void DisablePause()
        {
            pauseButton.Attributes.Remove("onmouseover");
            pauseButton.Attributes.Remove("onmouseout");
            pauseButton.Attributes.Remove("onmousedown");
            pauseButton.Attributes.Remove("onmouseup");
            pauseButton.ImageUrl = "/umbraco/images/MailQueue/Pause-Disabled-48x48.png";
            pauseButton.Enabled = false;
        }

        protected void GetSettings()
        {
            string sqlCmdText = "SELECT [smtpserver] " +
                  ",[smtpuser] " +
                  ",[smtppass] " +
                  ",[smtpport] " +
                  ",[smtpssl] " +
                  ",[smtponbehalf] " +
                  ",[smtpdummy] " +
                  ",[pause] " +
                  ",[maxbactchsize] " +
                  ",[sendinterval] " +
                  ",[retries] " +
                  ",[retryinterval] " +
                  ",[purgeafterdays] " +
                  ",[settingsname]" +
                  "FROM [Settings] WHERE iscurrentsetting = 1";

            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = sqlCmdText;
            cmd.CommandType = System.Data.CommandType.Text;

            SqlDataReader dr = DbHelper.DbHelper.ExecuteSqlReader(cmd, System.Configuration.ConfigurationManager.ConnectionStrings["mailqueue"].ConnectionString);

            using (dr)
            {
                while (dr.Read())
                {
                    for (int i = 0; i < 14; i++)
                    {
                        if (!dr.IsDBNull(i))
                        {
                            switch (i)
                            {
                                case 0:
                                    smtpserver = (string)dr[i];
                                    break;
                                case 1:
                                    smtpuser = (string)dr[i];
                                    break;
                                case 2:
                                    smtppass = (string)dr[i];
                                    break;
                                case 3:
                                    smtpport = (int)dr[i];
                                    break;
                                case 4:
                                    smtpssl = (bool)dr[i];
                                    break;
                                case 5:
                                    smtponbehalf = (string)dr[i];
                                    break;
                                case 6:
                                    smtpdummy = (string)dr[i];
                                    break;
                                case 7:
                                    pause = (bool)dr[i];
                                    break;
                                case 8:
                                    maxbactchsize = (int)dr[i];
                                    break;
                                case 9:
                                    sendinterval = (int)dr[i];
                                    break;
                                case 10:
                                    retries = (int)dr[i];
                                    break;
                                case 11:
                                    retryinterval = (int)dr[i];
                                    break;
                                case 12:
                                    purgeafterdays = (int)dr[i];
                                    break;
                                case 13:
                                    settingsname = (string)dr[i];
                                    break;
                            }
                        }
                    }
                }
            }

            dr.Close();
        }

        private void DisplaySettings()
        {
            tbMaxBatchSize.Text = maxbactchsize.ToString();
            tbRetries.Text = retries.ToString();
            tbRetryInterval.Text = retryinterval.ToString();
            tbSendInterval.Text = sendinterval.ToString();
            tbSendOnBehalfOf.Text = smtponbehalf;
            tbSmtpPassword.Text = smtppass;
            tbSmtpPort.Text = smtpport.ToString();
            tbSmtpServer.Text = smtpserver;
            tbSmtpUser.Text = smtpuser;
            tbSettingsName.Text = settingsname;
            cbUseSsl.Checked = smtpssl;

            if (pause)
            {
                EnableStart();
                DisablePause();
            }
            else
            {
                EnablePause();
                DisableStart();
            }
        }

        protected void pauseButton_Click(object sender, ImageClickEventArgs e)
        {
            EnableStart();
            DisablePause();

            SavePauseStatus(true);
        }

        protected void startButton_Click(object sender, ImageClickEventArgs e)
        {
            EnablePause();
            DisableStart();

            SavePauseStatus(false);
        }

        protected void SavePauseStatus(bool pause)
        {
            string sqlCmdText = "UPDATE Settings SET Pause = @pause WHERE settingsname = @name";

            SqlCommand cmd = new SqlCommand();
            cmd.Parameters.AddWithValue("@pause", pause);
            cmd.Parameters.AddWithValue("@name", settingsname);
            cmd.CommandText = sqlCmdText;
            cmd.CommandType = System.Data.CommandType.Text;

            DbHelper.DbHelper.ExecuteSqlNonQuery(cmd, System.Configuration.ConfigurationManager.ConnectionStrings["mailqueue"].ConnectionString);
        }

        protected void SaveSettings()
        {
            Int32.TryParse(tbMaxBatchSize.Text, out maxbactchsize);
            Int32.TryParse(tbRetries.Text, out retries);
            Int32.TryParse(tbRetryInterval.Text, out retryinterval);
            Int32.TryParse(tbSendInterval.Text, out sendinterval);
            Int32.TryParse(tbSmtpPort.Text, out smtpport);
            smtponbehalf = tbSendOnBehalfOf.Text;
            smtppass = tbSmtpPassword.Text;
            smtpserver = tbSmtpServer.Text;
            smtpuser = tbSmtpUser.Text;
            smtpssl = cbUseSsl.Checked;
            settingsname = tbSettingsName.Text;

            string sqlCmdText = "UPDATE Settings SET [smtpserver] = @smtpserver, [smtpuser] = @smtpuser, [smtppass] = @smtppass, " +
                " [smtpport] = @smtpport, [smtpssl] = @smtpssl, [smtponbehalf] = @smtponbehalf, [smtpdummy] = @smtpdummy, [maxbactchsize]=@maxbactchsize, " +
                " [sendinterval]=@sendinterval, [retries]=@retries, [retryinterval]=@retryinterval, [purgeafterdays]=@purgeafterdays WHERE settingsname = @name";

            SqlCommand cmd = new SqlCommand();
            cmd.Parameters.AddWithValue("@smtpserver", smtpserver);
            cmd.Parameters.AddWithValue("@smtpuser", smtpuser);
            cmd.Parameters.AddWithValue("@smtppass", smtppass);
            cmd.Parameters.AddWithValue("@smtpport", smtpport);
            cmd.Parameters.AddWithValue("@smtpssl", smtpssl);
            cmd.Parameters.AddWithValue("@smtponbehalf", smtponbehalf);
            cmd.Parameters.AddWithValue("@smtpdummy", smtpdummy);
            cmd.Parameters.AddWithValue("@maxbactchsize", maxbactchsize);
            cmd.Parameters.AddWithValue("@sendinterval", sendinterval);
            cmd.Parameters.AddWithValue("@retries", retries);
            cmd.Parameters.AddWithValue("@retryinterval", retryinterval);
            cmd.Parameters.AddWithValue("@purgeafterdays", purgeafterdays);
            cmd.Parameters.AddWithValue("@name", settingsname);
            cmd.CommandText = sqlCmdText;
            cmd.CommandType = System.Data.CommandType.Text;

            DbHelper.DbHelper.ExecuteSqlNonQuery(cmd, System.Configuration.ConfigurationManager.ConnectionStrings["mailqueue"].ConnectionString);

            DisplaySettings();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            SaveSettings();
            lblSave.Text = "Settings saved...";
        }
    }
}